﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
using System.Configuration;
using System.ComponentModel;


namespace AdministrationWeb.Administration
{
    public partial class AddMentor : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            TbtFirstName.Focus();
            if (Page.IsPostBack) return;
            //BindPathway();
            lblMentorId.Text = "";
            if (MySession.Current.mentorMentorID != null)
            {
                lblTitle.Text = "Edit Mentor";
                //set id to a label you create
                TbtFirstName.Text = MySession.Current.mentorFirstName;
                TbtLastName.Text = MySession.Current.mentorLastName;
                DdlGender.SelectedValue = MySession.Current.mentorGender;
                TbtEmail.Text = MySession.Current.mentorEmail;
                DdlDegree.SelectedValue = MySession.Current.mentorDegree;
                DdlYearsExperience.SelectedValue = MySession.Current.mentorYearsExperience;
                TbtCollege.Text = MySession.Current.mentorCollege;
                ddlFocus.DataBind();
                ddlFocus.SelectedValue = MySession.Current.mentorFocusID;
                BindPathway();
                ddlPathway.SelectedValue = MySession.Current.mentorPathwayID;
                txtComment.Text = MySession.Current.mentorAdditionalComments;
                DdlJobType.SelectedValue = MySession.Current.mentorJobType;
                DdlLocation.SelectedValue = MySession.Current.mentorLocation;
                DdlBenefCourse1.SelectedValue = MySession.Current.mentorBeneficialCourse1;
                DdlBenefCourse2.SelectedValue = MySession.Current.mentorBeneficialCourse2;
                DdlBenefCourse3.SelectedValue = MySession.Current.mentorBeneficialCourse3;
                imgMentorFace.ImageUrl = "/Administration/ShowMentorImage.ashx?id=" + MySession.Current.mentorMentorID;

                lblMentorId.Text = MySession.Current.mentorMentorID;

                MySession.Current.mentorMentorID = null;
            }
            else
            {
                lblTitle.Text = "Add Mentor";
                imgMentorFace.Visible = false;
            }
        }

        protected void BtnSave_Click(object sender, EventArgs e)
        {
            if (IsValid)
            {
                MySqlConnection conn;
                MySqlCommand comm;
                string connectionString = ConfigurationManager.ConnectionStrings["ApplicationConnectionString"].ConnectionString;
                conn = new MySqlConnection(connectionString);

                if (!string.IsNullOrEmpty(lblMentorId.Text))
                {
                    if (ImageUploader.HasFile)
                    {
                        comm = new MySqlCommand("UPDATE mentor SET first_name = ?TbtFirstName, last_name = ?TbtLastName, gender = ?DdlGender, "
                                                + "email = ?TbtEmail, photo = ?ImageUploader,degree = ?DdlDegree, college = ?TbtCollege, "
                                                + "years_experience_group = ?DdlYearsExperience,job_type = ?DdlJobType, location = ?DdlLocation, "
                                                + "benneficial_course1 = ?DdlBenefCourse1, benneficial_course2 = ?DdlBenefCourse2, benneficial_course3 = ?DdlBenefCourse3, "
                                                + "additional_comments = ?TxtComment, pathway_id = ?pathway_id "
                                                + "WHERE mentor_id = ?id", conn);

                        comm.Parameters.AddWithValue("?id", lblMentorId.Text);
                        comm.Parameters.AddWithValue("?ImageUploader", ImageUploader.FileBytes);
                    }
                    else
                    {
                        comm = new MySqlCommand("UPDATE mentor SET first_name = ?TbtFirstName, last_name = ?TbtLastName, gender = ?DdlGender, "
                                               + "email = ?TbtEmail, degree = ?DdlDegree, college = ?TbtCollege, "
                                               + "years_experience_group = ?DdlYearsExperience,job_type = ?DdlJobType, location = ?DdlLocation, "
                                               + "benneficial_course1 = ?DdlBenefCourse1, benneficial_course2 = ?DdlBenefCourse2, benneficial_course3 = ?DdlBenefCourse3, "
                                               + "additional_comments = ?TxtComment, pathway_id = ?pathway_id "
                                               + "WHERE mentor_id = ?id", conn);

                        comm.Parameters.AddWithValue("?id", Convert.ToInt32(lblMentorId.Text));
                    }
                }
                else
                {
                    comm = new MySqlCommand("INSERT INTO mentor (creation_user_id, first_name, last_name, gender, email, photo,degree, college,years_experience_group,job_type,location, benneficial_course1, benneficial_course2, benneficial_course3, additional_comments,creation_date, pathway_id) " +
                    "VALUES (?creation_user_id, ?TbtFirstName, ?TbtLastName, ?DdlGender,?TbtEmail, ?ImageUploader, ?DdlDegree,?TbtCollege, ?DdlYearsExperience,?DdlJobType,?DdlLocation, ?DdlBenefCourse1,?DdlBenefCourse2, ?DdlBenefCourse3,?TxtComment,?creation_date, ?pathway_id)", conn);

                    comm.Parameters.AddWithValue("?creation_date", DateTime.Now);
                    comm.Parameters.AddWithValue("?creation_user_id", MySession.Current.userUserID);
                    comm.Parameters.AddWithValue("?ImageUploader", ImageUploader.FileBytes);
                }

                comm.Parameters.AddWithValue("?TbtFirstName", TbtFirstName.Text);
                comm.Parameters.AddWithValue("?TbtLastName", TbtLastName.Text);
                comm.Parameters.AddWithValue("?DdlGender", DdlGender.SelectedItem.Value);
                comm.Parameters.AddWithValue("?TbtEmail", TbtEmail.Text);
                comm.Parameters.AddWithValue("?DdlDegree", Convert.ToInt32(DdlDegree.SelectedItem.Value));
                comm.Parameters.AddWithValue("?DdlJobType", Convert.ToInt32(DdlJobType.SelectedItem.Value));
                comm.Parameters.AddWithValue("?DdlLocation", Convert.ToInt32(DdlLocation.SelectedItem.Value));
                comm.Parameters.AddWithValue("?TbtCollege", TbtCollege.Text);
                comm.Parameters.AddWithValue("?DdlYearsExperience", Convert.ToInt32(DdlYearsExperience.SelectedItem.Value));

                if (DdlBenefCourse1.Text != "")
                    comm.Parameters.AddWithValue("?DdlBenefCourse1", Convert.ToInt32(DdlBenefCourse1.SelectedItem.Value));
                else
                    comm.Parameters.AddWithValue("?DdlBenefCourse1", null);

                if (DdlBenefCourse2.Text != "")
                    comm.Parameters.AddWithValue("?DdlBenefCourse2", Convert.ToInt32(DdlBenefCourse2.SelectedItem.Value));
                else
                    comm.Parameters.AddWithValue("?DdlBenefCourse2", null);

                if (DdlBenefCourse3.Text != "")
                    comm.Parameters.AddWithValue("?DdlBenefCourse3", Convert.ToInt32(DdlBenefCourse3.SelectedItem.Value));
                else
                    comm.Parameters.AddWithValue("?DdlBenefCourse3", null);

                comm.Parameters.AddWithValue("?TxtComment", txtComment.Text);
                comm.Parameters.AddWithValue("?pathway_id", Convert.ToInt32(ddlPathway.SelectedItem.Value));

                try
                {
                    conn.Open();
                    comm.ExecuteNonQuery();

                    Server.Transfer("UpdateMentorSuccess.aspx");
                }
                catch (Exception ex)
                {
                    LblResult.Text += "There was an error submitting request " + ex.Message;

                }
                finally
                {
                    conn.Close();
                }
            }
        }

        protected void BtnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Administration/default.aspx");
        }

        protected void ImageValidation_ServerValidate(object source, ServerValidateEventArgs args)
        {
            if ((ImageUploader.FileName.EndsWith(".jpg") || ImageUploader.FileName.EndsWith(".jpeg")) && ImageUploader.FileName != "" && ImageUploader.FileContent.Length < 1030000) args.IsValid = true;
            else args.IsValid = false;
        }

        protected void BindPathway()
        {
            const string sqlBase = "SELECT lkup_pathway.pathway_id, lkup_pathway.pathway_name FROM lkup_pathway WHERE lkup_pathway.focus_id = @focus_id ORDER BY pathway_name";
            var sql = sqlBase.Replace("@focus_id", ddlFocus.SelectedValue);
            SqlPathway.SelectCommand = sql;
            ddlPathway.DataBind();
        }

        protected void UpdateImageValidation_ServerValidate(object source, ServerValidateEventArgs args)
        {
            if ((ImageUploader.FileName.EndsWith(".jpg") || ImageUploader.FileName.EndsWith(".jpeg")) && ImageUploader.FileContent.Length < 1030000) args.IsValid = true;
            else args.IsValid = false;
        }

        protected void ddlFocus_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindPathway();
        }
    }
}